<?php

namespace app\api\controller\wanlshop;
use app\common\controller\Api;
use think\Db;
/**
 * WanlShop充值卡接口
 */
class Chargecard extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function charge(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {

            if(!$this->auth->id||$this->auth->id<1){
                $this->error(__('请登录'));
            }

            $card_no=trim($this->request->post("card_no"));
            $cardpass=trim($this->request->post('cardpass'));
            if(!preg_match('/^[a-zA-Z0-9]+$/', $card_no)){
                $this->error(__('充值卡号有误'));
            }
            if(!preg_match('/^\d+$/', $cardpass)){
                $this->error(__('充值卡密码有误'));
            }

            $card =model('app\common\model\wanlshop\ChargeCard');
            $condition_card=['card_no'=>$card_no];
            $cardinfo =$card->where($condition_card)->field('id,card_no,user_id,shop_id,is_bind,balance_money,activetime,status,cardpass')->find();
            if(!$cardinfo){
                $this->error(__('充值卡号有误'));
            }
            if(trim($cardinfo['cardpass'])!=$cardpass){
                $this->error(__('卡号密码有误'));
            }
            if($cardinfo['status']!=1){
                $this->error(__('该卡未被激活'));
            }
            if($cardinfo['activetime']>0||$cardinfo['user_id']>0||$cardinfo['is_bind']>0){
                $this->error(__('该卡已被使用'));
            }
            if($cardinfo['balance_money']==0) {
                $this->error(__('该卡无效'));
            }

            $time=time();
            /*$cachedata=cache($card_no.''.$this->auth->id);
            if(!$cachedata){
                $cachedata['num']=0;
                $cachedata['time']=$time;
                cache($card_no.''.$this->auth->id,$cachedata,3600);
            }

            if(!$cachedata){
                $this->error(__('系统错误'));
            }
            if($cachedata['num']>6 && $time-$cachedata['time']<3600){
                $this->error(__("充值卡密码输入错误".$cachedata['num']."次，请60分钟以后再试"));
            }
            if($cachedata['num']>4 && $time-$cachedata['time']<1800){
                $this->error(__("充值卡密码输入错误".$cachedata['num']."次，请30分钟以后再试"));
            }
            if($cachedata['num']>2 && $time-$cachedata['time']<300){
                $this->error(__("充值卡密码输入错误".$cachedata['num']."次，请5分钟以后再试"));
            }

            if($cardinfo['cardpass']!=$cardpass){

                $cachedata['num']=$cachedata['num']+1;
                $error="充值卡密码错误，请重新输入";
                if($cachedata['num']>6){
                    $error="充值卡密码输入错误".$cachedata['num']."次，请60分钟以后再试";
                }
                elseif($cachedata['num']>4){
                    $error="充值卡密码输入错误".$cachedata['num']."次，请30分钟以后再试";
                }
                elseif($cachedata['num']>2){
                    $error="充值卡密码输入错误".$cachedata['num']."次，请5分钟以后再试";
                }
                $cachedata['time']=$time;
                cache($card_no.''.$this->auth->id,$cachedata,3600);
                $this->error(__($error));
            }
            cache($card_no.''.$this->auth->id,null);
            */

            $user=model('app\common\model\User')->where(['id'=>$this->auth->id])->find();

            if(empty($user)){
                $this->error(__('用户不存在'));
            }

            //绑定充值卡数据
            $card_data['activetime'] = $time;
            $card_data['is_bind'] = 1;
            $card_data['user_id'] =$this->auth->id;

            //充值日志数据
            $logdata['card_no']=$cardinfo['card_no'];
            $logdata['user_id']=$this->auth->id;
            $logdata['shop_id']=$cardinfo['shop_id'];
            $logdata['des']='用户ID ['.$this->auth->id.']输入卡号充值，金额共'. $cardinfo['balance_money'];

            Db::startTrans();
            try {
                //数据库操作
                $card->where($condition_card)->update($card_data);
                $user->save(['card_no'=>$cardinfo['card_no']]);
                $user->money($cardinfo['balance_money'], $user['id'],'充值卡充值','recharge');
                model('app\common\model\wanlshop\ChargeCardLog')->create($logdata);
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                $this->error(__($e->getMessage()));
            }
            $this->success(__('充值成功'));
        }
        $this->error(__('非法请求'));
    }

}